﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace JULONG.WEB.Library
{
    public static partial class staticClass
    {
        /// <summary>
        /// 返回路由值
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="key">area controller action id</param>
        /// <returns></returns>
        public static String getRoute(this HtmlHelper htmlHelper, routeKeyEnum routeKey)
        {
            var routeData = htmlHelper.ViewContext.RouteData;

            if (routeKey == routeKeyEnum.area)
            {
                var dataTokens = routeData.DataTokens;
                if (dataTokens.ContainsKey("area"))
                    return dataTokens["area"].ToString();

            }

            return routeData.Values[routeKey.ToString()].ToTryString();
        }

        public static String getRoute(this HtmlHelper htmlHelper, string routeKeyName)
        {
            var routeData = htmlHelper.ViewContext.RouteData;
            if (routeKeyName == "area")
            {
                var dataTokens = routeData.DataTokens;
                if (dataTokens.ContainsKey("area"))
                    return dataTokens["area"].ToString();
            }
            return routeData.Values[routeKeyName].ToTryString();
        }


        public static System.Web.HtmlString newLineToBR(this HtmlHelper htmlHelper, string str)
        {
            if(string.IsNullOrWhiteSpace(str) ) return new System.Web.HtmlString(str);
            return new System.Web.HtmlString(string.Join("<br/>", str.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None)));
        }

    }

    public enum routeKeyEnum
    {
        controller,
        action,
        area,
        id
    }
}
